package com.meilai.project.core.excel;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.system.SystemUtil;
import com.meilai.project.core.excel.config.QueryParamConfig;
import com.meilai.project.core.excel.config.TableHeaderConfig;
import lombok.Data;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author xchen
 * @since 2022-02-16
 */
public class ExcelTest {
	public static void main(String[] args) throws IOException {

		//配置导出
		ReportSheet reportSheet = ReportSheet.create("测试文件导出")
				.addQueryParamConfig(queryParamConfigList())
				.addTableHeaderConfig(tableHeaderConfigList());

		ExcelGenerator generator = ExcelGenerator.createExcel().sheet(reportSheet);
		//模拟分页
		for (int i = 0; i < 5; i++) {
			List<Person> dataList = queryByPage(i);
			generator.write(dataList);
		}

		File f = new File(SystemUtil.getUserInfo().getHomeDir() + "/test.xls");
		f.delete();
		f.createNewFile();
		FileOutputStream fileOutputStream = new FileOutputStream(f);
		generator.save(fileOutputStream);
	}

	private static List<Person> queryByPage(Integer page) {
		return CollUtil.newArrayList(Person.of("张三" + page, "男", 20, new Date()), Person.of("小红" + page, "女", 18, new Date()));
	}

	private static List<QueryParamConfig> queryParamConfigList() {
		List<QueryParamConfig> config = new ArrayList<>();
		config.add(QueryParamConfig.of("条件1", "哈哈哈"));
		config.add(QueryParamConfig.of("条件2", "呵呵呵"));
		return config;
	}

	private static List<TableHeaderConfig> tableHeaderConfigList() {
		List<TableHeaderConfig> config = new ArrayList<>();
		config.add(TableHeaderConfig.of("姓名", "name"));
		config.add(TableHeaderConfig.of("性别", "gender"));
		config.add(TableHeaderConfig.of("年龄", "age"));
		//这里的宽度是字符数，比如这里是指20个字符宽度
		config.add(TableHeaderConfig.of("生日", "birth", 20));
		return config;
	}

	@Data
	public static class Person {
		private String name;
		private String gender;
		private Integer age;
		private Date birth;

		public static Person of(String name, String gender, Integer age, Date date) {
			Person person = new Person();
			person.setName(name);
			person.setAge(age);
			person.setGender(gender);
			person.setBirth(date);
			return person;
		}
	}
}
